﻿using HomagChina.Basic.CCT.Tools;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using HomagGroup.Base.UI.Windows;
using Proxy;
using System;
using System.Collections.Generic;
using System.Linq;
using static HomagChina.Basic.CCT.Tools.StackInfoConstruct;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class JobImportViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private ModifyInfoHandle Handle_Modify;
        private CommonHandle Handle_Common;
        public RelayCommand NextCommand { get; set; }

        #endregion Handle

        #region 属性

        private StackInfoConstruct stackInfoConstruct;
        private List<StackDetail> _List_StackDetail;
        private string _MyFilename;

        public List<StackDetail> List_StackDetail
        {
            get => _List_StackDetail;
            set { _List_StackDetail = value; OnPropertyChanged(); }
        }


        public DropDownList DropDownList_TargetPostion { get; set; }

        public string MyFilename
        {
            get => _MyFilename;
            set { _MyFilename = value; OnPropertyChanged(); }
        }

        private string _Title;

        public string Title
        {
            get => _Title;
            set { _Title = value; OnPropertyChanged(); }
        }

        #endregion 属性

        private RelayEditCommand _FileSelectCommand;

        public RelayEditCommand FileSelectCommand
        {
            get => _FileSelectCommand;
            set { _FileSelectCommand = value; OnPropertyChanged(); }
        }

        public JobImportViewModel()
        {
            Title = GetLocalText("Import");
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Common = new CommonHandle();
            NextCommand = new RelayCommand(Next);
            FileSelectCommand = new RelayEditCommand(FileSelectExecute);
            //LoadDropDownList();
        }

        public string FileSelectExecute(object actualValue, object parameter)
        {
            string fileName = string.Empty;
            try
            {
                string format = "csv files (*.csv, *.csv)|*.csv;*.csv|All files (*.*)|*.*";

                var data = new FileDialogData()
                {
                    InitialDirectory = Globle.ServerName == "Local" ?
                    "C:\\Users\\tb28who\\Desktop\\Homag\\Homag Project Info\\Horizontal Storage\\Database\\Job import sample data\\Job import sample data"
                    : @"E:\Software\Ex Data",
                    Filter = format,
                    Features = FileDialogData.FeaturesModes.CanShowPreview,
                };
                if (UIAdjustments.CurrentThreadDialogManager.FileDialog.SelectFiles(System.Windows.Application.Current.MainWindow, data) == true)
                {
                    fileName = data.FileName.Split('\\').Last();
                    stackInfoConstruct = new DealWithFiles().DealWithJobStackInfoFiles(data.FileName, ',', "csv");
                    List_StackDetail = stackInfoConstruct.stackDetails;
                }

                return fileName;
            }
            catch (Exception ex)
            {
                return fileName;
            }
        }

        private void LoadDropDownList()
        {
            DropDownList_TargetPostion = new DropDownList
            {
                ItemsSource = (Handle_Search.GetOutfeedInfo())
            };
            DropDownList_TargetPostion.SelectedItem = DropDownList_TargetPostion.Items.CurrentItem;
            DropDownList_TargetPostion.SelectedIndex = 0;
        }


        public void Next()
        {
            var returnMsg = Handle_Insert.InsertJobInfo(stackInfoConstruct, DropDownList_TargetPostion.SelectedValue);
            HomagGroup.Base.UI.Windows.MessageBox.Show(returnMsg.Msg);
            Handle_Common.CloseDiaLog(Title);
        }
    }
}